#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using SqEngine.Components;
using sqengine.Components;
using sqengine.Engine.Common;
#endregion

namespace sqengine.Components
{
    public class GPSSystem : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Game _game;
        private SpriteFont sb_fnt;
        private string _font;
        private bool _enabled;
        private SpriteBatch _spritebatch;
        private string _playername;
        private AdvancedContent _adcontent;
        private Vector3 _playerposition;
        private int bbWidth;
        private int bbHeight;

        public GPSSystem(Game game, string font, AdvancedContent adcontent)
            : base(game)
        {
            _game = game;
            _font = font;
            _adcontent = adcontent;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            bbWidth = _game.GraphicsDevice.PresentationParameters.BackBufferWidth;
            bbHeight = _game.GraphicsDevice.PresentationParameters.BackBufferHeight;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (_enabled)
            {
                if (_spritebatch == null)
                    _spritebatch = new SpriteBatch(_game.GraphicsDevice);

                if (sb_fnt == null)
                    sb_fnt = _adcontent.LoadSpriteFont(_font);

                if (_playername != "")
                {
                    string strpos = string.Format("{0}: {1}", _playername, _playerposition);
                    _spritebatch.Begin();
                    _spritebatch.DrawString(sb_fnt, strpos, new Vector2(bbWidth - sb_fnt.MeasureString(strpos).X, 30), Color.Red);
                    _spritebatch.DrawString(sb_fnt, strpos, new Vector2(bbWidth - sb_fnt.MeasureString(strpos).X + 1, 30), Color.White);
                    _spritebatch.End();
                    ((sqengine)_game).RestoreRenderState();
                }
                
            }

            base.Draw(gameTime);
        }

        public void Active()
        {
            _enabled = true;

        }

        public void Disable()
        {
            _enabled = false;
        }

        public void UpdatePosition(string player_name, Vector3 position)
        {
            _playername = player_name;
            _playerposition = position;

        }
    }
}